In [4]:
"""
pySVD_eDNA by Eshani Hettiarachchi, Chemistry & Biochemitry at University of California San Diego. 

Determination of eDNA melting curves using ATR-FTIR spectra collected at different temperatures 
by Single Value Decomposition/ SVD Analysis. 
The matrix n x m, where n/ rows represent the spectral region and m/ columns represent the absorbance 
for each wavenumber at different temperatures.
The singular values (second component from the SVD analysis) represent the patterns of spectral changes 
arising from the DNA melting.

This program was first used in 

Hettiarachchi, E. et al, Impact of Surface Adsorption on DNA Structure and Stability: 
Implications for Environmental DNA Interactions with Iron Oxide Surfaces.
Langmuir 2024, 40, 52, 27194–27205

https://pubs.acs.org/doi/full/10.1021/acs.langmuir.4c02501

"""

print("")
user_name = input("Enter your name: ") 
print("")
print("Hello", user_name)
print("")
print("\033[95m\033[95m")
print("")

import numpy as np
import pandas as pd
import sys
import matplotlib.pyplot as plt

file_name = input("Enter your CSV file name (for analysing the sample dataset provided, enter: nommlt.csv): ")

# Read CVS, no headers
try:
  df = pd.read_csv(file_name, header=None)
  A = df.values  # Convert DataFrame to NumPy array
  print("Matrix loaded successfully:")
  print(A)
except Exception as e:
  print(f"Error loading matrix: {e}")
  exit(1)

# Perform SVD

try:
  U, S, Vt = np.linalg.svd(A)
  print("U matrix:")
  print(U)
  print("Singular values:")
  print(S)
  print("Vt matrix:")
  print(Vt)
except Exception as e:
  print(f"Error performing SVD: {e}")
  exit(1)
    
# Sort the singular values from maximum to minimum
sorted_S = np.sort(S)[::-1]

# Print the sorted singular values in a column
print("Sorted Singular Values:")
for sv in sorted_S:
    print(sv)
    
# Define the custom X axis values for plotting
# Assuming you have a range of X axis values from 1 to the number of singular values
x_axis_values = np.arange(1, len(sorted_S) + 1)

# Plot the sorted singular values against your custom X axis, so you see whether matrix decomposed correctly.
plt.plot(x_axis_values, sorted_S, marker='o', linestyle='-')
plt.xlabel('Custom X Axis')
plt.ylabel('Singular Values')
plt.title('Sorted Singular Values')
plt.grid(True)
plt.show()

# Sort and print the third component from maximum to minimum for plotting against temperature for sigmoidal melting curve. 
sorted_Vt = np.sort(Vt)[::-1]


print("Sorted Vt:")
for vt in sorted_Vt:
    print(vt)

print("")
print("Analysis complete. Tables, counts, and plots generated.")
print("")
print("\033[94mExtract the first dataset of Sorted Vt values, and plot them in y axis against the temperatures.\033[94m")
print("\033[31mYou may find this information just below the plot of Sorted Singular Values in the output section.\033[31m") 
print("")

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

print("How your data would look like for the sample dataset provided (nommlt.csv)")

img = mpimg.imread('nommlt.png')
plt.figure(figsize=(8, 10))
plt.imshow(img)
plt.axis('off')
plt.show()

print("\033[94m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\033[94m")
print("\033[94mpySVD_eDNA for DNA melting curve calculation (2024) by Eshani Hettiarachchi, Ph.D.\033[94m") 

from datetime import date
today = date.today()
print(f"\033[93mAnalysis run by: {user_name}\033[93m")
print("\033[95m\033[95m")
print(today)

print("")

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('flowchart.jpeg')
plt.figure(figsize=(5, 5))
plt.imshow(img)
plt.axis('off')
plt.show()

# End 

Hello E.H.



Matrix loaded successfully:
[[0.26279 0.28698 0.28793 ... 0.17722 0.17757 0.17618]
 [0.26594 0.28986 0.29082 ... 0.18154 0.18187 0.1805 ]
 [0.26731 0.29091 0.29187 ... 0.18416 0.18448 0.18314]
 ...
 [0.02384 0.01338 0.00943 ... 0.06453 0.06455 0.06011]
 [0.02392 0.01349 0.00954 ... 0.06451 0.06453 0.06009]
 [0.02399 0.01359 0.00963 ... 0.06449 0.0645  0.06006]]
U matrix:
[[-1.98196019e-02  6.39361190e-02  3.20677358e-02 ... -5.53408647e-04
  -5.03158554e-04 -4.20616620e-04]
 [-2.00584342e-02  6.31619080e-02  3.11935694e-02 ...  2.22699381e-02
   2.19310574e-02  2.20861549e-02]
 [-2.01634174e-02  6.23111204e-02  3.04948022e-02 ... -1.75849052e-02
  -1.76146038e-02 -1.73125862e-02]
 ...
 [-2.14380836e-03 -2.36025305e-02  2.01071701e-02 ...  9.96645069e-01
  -3.34332079e-03 -3.31833208e-03]
 [-2.14940923e-03 -2.35343915e-02  2.01269615e-02 ... -3.31903228e-03
   9.96650170e-01 -3.34078288e-03]
 [-2.15433914e-03 -2.34672441e-02  2.01583109e-02 ... -3.25791046e-03
  -3.29499572e-03  9.96639426e-01]]
Singular values:
[5.79701090e+01 4.95553568e+00 6.61297842e-01 1.30071714e-01
 8.80521100e-02 7.23676306e-02 1.57806484e-02 1.19937735e-02
 9.15261542e-03 7.20297338e-03 5.54369510e-03 3.73836998e-03
 2.59054287e-03 2.26103250e-03 1.90146154e-03 1.64032116e-03
 1.49092662e-03 1.28348648e-03 1.27200169e-03]
Vt matrix:
[[-2.28420482e-01 -2.26663498e-01 -2.25610581e-01 -2.26477211e-01
  -2.26141871e-01 -2.25940309e-01 -2.25712088e-01 -2.26844198e-01
  -2.27686347e-01 -2.28592049e-01 -2.29491811e-01 -2.32885766e-01
  -2.32813990e-01 -2.33046350e-01 -2.31307066e-01 -2.33163806e-01
  -2.32981662e-01 -2.33031506e-01 -2.31730865e-01]
 [ 1.32802733e-02  1.00722193e-01  1.15380149e-01  1.22982931e-01
   1.47352106e-01  1.64049849e-01  2.01174938e-01  2.18956081e-01
   2.30077568e-01  2.39665142e-01  2.47252105e-01 -2.88561550e-01
  -2.73308701e-01 -2.93215379e-01  2.63882223e-01 -3.01693763e-01
  -2.88597733e-01 -2.89027741e-01 -2.85088042e-01]
 [-1.77807013e-01 -2.29261016e-01 -3.48844547e-01 -2.31951539e-01
  -2.36953755e-01 -2.40311515e-01 -2.06256291e-01 -1.87480403e-02
   1.08657209e-01  2.32495086e-01  3.42351422e-01  6.21188333e-02
   4.32882398e-02  7.56055324e-02  5.99456016e-01  9.79566985e-02
   8.48147183e-02  8.55254102e-02 -7.46681662e-02]
 [ 6.98281640e-01 -2.39915790e-02  7.22163158e-02 -5.52528978e-03
  -5.65100197e-02 -3.14007025e-02 -1.27427246e-01 -2.18957118e-01
  -1.99501953e-01 -1.05859488e-01  5.19986034e-02  9.11545922e-02
   3.88226673e-01  5.82526253e-02  1.93005657e-01 -2.67745096e-01
  -1.04606089e-01 -1.16536847e-01 -2.97702264e-01]
 [ 1.59367761e-01  1.24900252e-01 -8.48584479e-01  1.46205916e-01
   1.64032213e-01  1.74780914e-01  1.68683212e-01  8.83692332e-02
   4.41686053e-02  1.43014650e-02  7.20703063e-04  1.22407713e-03
   3.26744987e-02  1.76871048e-02 -2.21276919e-01  2.94279318e-02
   8.02824014e-02  6.68263549e-02 -2.43995078e-01]
 [-1.28927500e-01  5.50078707e-03  2.93651071e-01 -3.80923222e-02
  -3.61348783e-02 -4.89728319e-02 -1.35078568e-02  1.25710071e-02
   5.10565688e-02  6.26224040e-02  5.48261051e-02  1.21692421e-02
  -2.94054804e-02  5.90490809e-02 -2.00178897e-01  1.98579025e-01
   2.99426708e-01  2.49885690e-01 -8.03730468e-01]
 [-4.81609357e-01  3.02118666e-01 -2.82675192e-02  5.11657912e-02
   3.13219400e-02 -1.43323506e-02  1.34023131e-01 -3.18236217e-02
   6.79380463e-02 -3.48871947e-02 -3.32825180e-01  3.72718640e-01
   3.35964189e-01  1.56208694e-01  2.06780066e-01 -3.73958040e-01
   2.62122896e-02 -2.28288558e-01 -1.58099310e-01]
 [ 4.33365361e-02 -4.49784065e-02 -1.48718883e-02  1.52935631e-01
   1.85608701e-01  1.43693523e-01  1.80780665e-03  4.18289673e-02
  -1.98653258e-01 -3.03328085e-01 -4.44347354e-01 -5.24553095e-02
  -2.89405635e-01  1.30767742e-01  5.27641942e-01  3.93347244e-01
  -1.49624725e-01  3.82060041e-02 -1.61674763e-01]
 [-4.50549262e-02 -6.41501659e-01  3.09503231e-02 -4.84246769e-02
   7.09305771e-02  1.44076442e-01  1.67518999e-01  2.25190488e-01
   7.43520317e-02  4.11412096e-03  9.06662522e-02  3.72200618e-01
   5.69795476e-02  2.89804167e-01 -1.97505866e-01  8.78417675e-02
  -3.66492903e-01 -2.21993187e-01 -9.34106746e-02]
 [-3.58796796e-01  1.00026190e-02  4.15458030e-03  3.63182387e-01
   2.51672362e-01  8.12655790e-02 -2.16945685e-01 -1.61271310e-01
  -2.55162596e-01 -1.13085478e-01  3.73696918e-01 -2.99100642e-01
   4.34769280e-01  1.88167573e-02 -3.16060615e-02  1.80427692e-01
  -2.55321722e-01  5.65887807e-03 -3.27048979e-02]
 [-4.41179739e-02 -4.72747049e-01  1.91678399e-02 -4.72852524e-02
   6.51860370e-02  1.86622636e-01  2.78990658e-01 -6.53022810e-02
   1.08936391e-01 -3.44732419e-02 -2.13971389e-01 -5.28660379e-01
   2.70480825e-01  8.30550086e-02  1.12937532e-01 -2.09944788e-01
   4.15023445e-01  2.32997531e-02  5.27729634e-02]
 [-8.78187151e-02 -1.60078113e-01  8.47972726e-03  9.93605771e-02
   1.99613638e-01  2.44800819e-01  1.62297379e-01 -4.76599053e-01
  -3.37081327e-01 -2.12158451e-02  2.91105731e-01  3.47432856e-01
  -3.50260822e-01 -1.41449747e-01  7.51602127e-02 -1.62987863e-01
   3.25586136e-01 -4.62417075e-02  2.97123904e-02]
 [-3.84506907e-02  2.56256554e-01  4.27973876e-03 -3.26581540e-01
  -6.34925659e-02  8.65152825e-02  1.61438241e-01  1.21921765e-01
  -4.12262158e-01  2.99849449e-02  2.04643729e-01 -2.45641904e-01
  -1.87369908e-01  6.55579420e-01 -2.89830550e-02 -1.77556600e-01
  -5.70831014e-02  8.84762203e-03  7.98211541e-03]
 [ 5.35055910e-02  1.81201643e-02 -8.39587324e-03  3.53088114e-01
  -9.55877153e-02  7.30568552e-03 -3.81113620e-01 -9.63345278e-02
   4.14515899e-01 -2.51895055e-01  1.20270345e-01 -7.08950169e-02
  -2.46828711e-01  4.21121682e-01 -5.92810046e-02 -5.18541682e-02
   2.48508071e-01 -3.83751660e-01  9.51865679e-03]
 [ 3.08537013e-02  4.87665888e-02 -2.50585967e-03  2.45707156e-01
  -4.68851945e-01 -1.81562504e-02  2.78057380e-01 -9.62267428e-02
  -2.54027271e-01  3.75026948e-01 -9.06318099e-02 -7.29523045e-02
   8.11066303e-02 -2.15235320e-02 -3.14519937e-02  3.86981176e-01
   1.09260790e-01 -4.90977642e-01 -8.41982885e-03]
 [-1.34880792e-02  1.91944966e-01  1.73556015e-03 -3.57060278e-01
  -1.52562969e-01  2.17440963e-01  3.33913188e-01 -4.87726283e-01
   4.05346418e-01 -3.11969220e-01  1.44052644e-01 -4.73597912e-02
   7.19491265e-02 -6.41698614e-03  1.36636509e-02  2.71581280e-01
  -2.24601609e-01 -2.99917006e-02 -2.04503818e-02]
 [ 2.81963419e-02  3.49557025e-02 -1.22375487e-03 -2.89417556e-01
   3.76280189e-01  2.51401437e-01 -3.85526657e-01 -3.13438079e-01
   9.13515368e-02  5.71691167e-01 -2.61477872e-01 -4.51420467e-02
  -2.44958327e-03  1.19053361e-01 -7.19011216e-02  1.09778802e-01
  -5.94672284e-02 -1.48376466e-01 -4.26484251e-03]
 [-6.14543334e-02  5.87825219e-02  3.99174400e-03 -2.35599621e-01
  -2.72880242e-01  6.68533332e-01 -3.32287446e-01  3.50045561e-01
  -1.21260650e-01 -1.83673519e-01  6.13370242e-02  7.23659605e-02
   1.23899620e-01 -2.37038870e-01  3.07643895e-02  3.80705865e-02
   1.80623332e-01 -1.37303403e-01 -6.85849701e-03]
 [ 4.60289315e-02  7.60270247e-02  1.65926459e-03 -3.55107732e-01
   4.68381808e-01 -3.49424517e-01  6.87570966e-02  2.03443716e-01
  -7.20000999e-02 -2.24419734e-01  1.38671177e-01 -1.08008150e-02
   9.32138730e-02 -1.26741791e-01  2.75370981e-03  2.37333722e-01
   2.89485677e-01 -4.88483090e-01  1.27371938e-03]]
Sorted Singular Values:
57.97010898767228
4.955535684805099
0.6612978419070504
0.13007171403215023
0.08805211001339952
0.07236763062263596
0.015780648434397165
0.011993773514872281
0.009152615417432956
0.007202973376582622
0.005543695098255351
0.0037383699833549956
0.002590542870565446
0.002261032499579754
0.0019014615412607967
0.0016403211570001318
0.0014909266188556836
0.001283486484824907
0.0012720016863210248
Sorted Vt:
[-0.48848309 -0.35510773 -0.34942452 -0.22441973 -0.12674179 -0.0720001
 -0.01080081  0.00127372  0.00165926  0.00275371  0.04602893  0.0687571
  0.07602702  0.09321387  0.13867118  0.20344372  0.23733372  0.28948568
  0.46838181]
[-0.33228745 -0.27288024 -0.23703887 -0.23559962 -0.18367352 -0.1373034
 -0.12126065 -0.06145433 -0.0068585   0.00399174  0.03076439  0.03807059
  0.05878252  0.06133702  0.07236596  0.12389962  0.18062333  0.35004556
  0.66853333]
[-0.38552666 -0.31343808 -0.28941756 -0.26147787 -0.14837647 -0.07190112
 -0.05946723 -0.04514205 -0.00426484 -0.00244958 -0.00122375  0.02819634
  0.0349557   0.09135154  0.1097788   0.11905336  0.25140144  0.37628019
  0.57169117]
[-0.48772628 -0.35706028 -0.31196922 -0.22460161 -0.15256297 -0.04735979
 -0.0299917  -0.02045038 -0.01348808 -0.00641699  0.00173556  0.01366365
  0.07194913  0.14405264  0.19194497  0.21744096  0.27158128  0.33391319
  0.40534642]
[-0.49097764 -0.46885194 -0.25402727 -0.09622674 -0.09063181 -0.0729523
 -0.03145199 -0.02152353 -0.01815625 -0.00841983 -0.00250586  0.0308537
  0.04876659  0.08110663  0.10926079  0.24570716  0.27805738  0.37502695
  0.38698118]
[-0.38375166 -0.38111362 -0.25189505 -0.24682871 -0.09633453 -0.09558772
 -0.07089502 -0.059281   -0.05185417 -0.00839587  0.00730569  0.00951866
  0.01812016  0.05350559  0.12027034  0.24850807  0.35308811  0.4145159
  0.42112168]
[-0.41226216 -0.32658154 -0.2456419  -0.18736991 -0.1775566  -0.06349257
 -0.0570831  -0.03845069 -0.02898306  0.00427974  0.00798212  0.00884762
  0.02998494  0.08651528  0.12192176  0.16143824  0.20464373  0.25625655
  0.65557942]
[-0.47659905 -0.35026082 -0.33708133 -0.16298786 -0.16007811 -0.14144975
 -0.08781872 -0.04624171 -0.02121585  0.00847973  0.02971239  0.07516021
  0.09936058  0.16229738  0.19961364  0.24480082  0.29110573  0.32558614
  0.34743286]
[-0.52866038 -0.47274705 -0.21397139 -0.20994479 -0.06530228 -0.04728525
 -0.04411797 -0.03447324  0.01916784  0.02329975  0.05277296  0.06518604
  0.08305501  0.10893639  0.11293753  0.18662264  0.27048083  0.27899066
  0.41502345]
[-0.3587968  -0.29910064 -0.25532172 -0.2551626  -0.21694568 -0.16127131
 -0.11308548 -0.0327049  -0.03160606  0.00415458  0.00565888  0.01000262
  0.01881676  0.08126558  0.18042769  0.25167236  0.36318239  0.37369692
  0.43476928]
[-0.64150166 -0.3664929  -0.22199319 -0.19750587 -0.09341067 -0.04842468
 -0.04505493  0.00411412  0.03095032  0.05697955  0.07093058  0.07435203
  0.08784177  0.09066625  0.14407644  0.167519    0.22519049  0.28980417
  0.37220062]
[-0.44434735 -0.30332809 -0.28940564 -0.19865326 -0.16167476 -0.14962472
 -0.05245531 -0.04497841 -0.01487189  0.00180781  0.038206    0.04182897
  0.04333654  0.13076774  0.14369352  0.15293563  0.1856087   0.39334724
  0.52764194]
[-0.48160936 -0.37395804 -0.33282518 -0.22828856 -0.15809931 -0.03488719
 -0.03182362 -0.02826752 -0.01433235  0.02621229  0.03132194  0.05116579
  0.06793805  0.13402313  0.15620869  0.20678007  0.30211867  0.33596419
  0.37271864]
[-0.80373047 -0.2001789  -0.1289275  -0.04897283 -0.03809232 -0.03613488
 -0.02940548 -0.01350786  0.00550079  0.01216924  0.01257101  0.05105657
  0.05482611  0.05904908  0.0626224   0.19857903  0.24988569  0.29365107
  0.29942671]
[-8.48584479e-01 -2.43995078e-01 -2.21276919e-01  7.20703063e-04
  1.22407713e-03  1.43014650e-02  1.76871048e-02  2.94279318e-02
  3.26744987e-02  4.41686053e-02  6.68263549e-02  8.02824014e-02
  8.83692332e-02  1.24900252e-01  1.46205916e-01  1.59367761e-01
  1.64032213e-01  1.68683212e-01  1.74780914e-01]
[-0.29770226 -0.2677451  -0.21895712 -0.19950195 -0.12742725 -0.11653685
 -0.10585949 -0.10460609 -0.05651002 -0.0314007  -0.02399158 -0.00552529
  0.0519986   0.05825263  0.07221632  0.09115459  0.19300566  0.38822667
  0.69828164]
[-0.34884455 -0.24031151 -0.23695376 -0.23195154 -0.22926102 -0.20625629
 -0.17780701 -0.07466817 -0.01874804  0.04328824  0.06211883  0.07560553
  0.08481472  0.08552541  0.0979567   0.10865721  0.23249509  0.34235142
  0.59945602]
[-0.30169376 -0.29321538 -0.28902774 -0.28859773 -0.28856155 -0.28508804
 -0.2733087   0.01328027  0.10072219  0.11538015  0.12298293  0.14735211
  0.16404985  0.20117494  0.21895608  0.23007757  0.23966514  0.24725211
  0.26388222]
[-0.23316381 -0.23304635 -0.23303151 -0.23298166 -0.23288577 -0.23281399
 -0.23173086 -0.23130707 -0.22949181 -0.22859205 -0.22842048 -0.22768635
 -0.2268442  -0.2266635  -0.22647721 -0.22614187 -0.22594031 -0.22571209
 -0.22561058]

Analysis complete. Tables, counts, and plots generated.

Extract the first dataset of Sorted Vt values, and plot them in y axis against the temperatures.
You may find this information just below the plot of Sorted Singular Values in the output section.

How your data would look like for the sample dataset provided (nommlt.csv)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pySVD_eDNA for DNA melting curve calculation (2024) by Eshani Hettiarachchi, Ph.D.
Analysis run by: E.H.

2025-10-31

In [ ]:
 
In [ ]: